﻿using System;
using System.Collections.Generic;
using System.Text;
using OpenGameEngine.GameObjects;
using OpenGameEngine.Drawing;
using OpenGameEngine.Audio;
using OpenGameEngine;
using System.IO;

namespace ChopperGame
{
    public class Chopper : GameRectangle
    {
        private float Acceleration;
        private float BladeCounter;
        private float BladeSpeed;
        private Bitmap[] HelicopterImages;
        public AudioPlayer FlyingSound { get; private set; }
        private float MaxGravity;
        private float MaxVelocity;
        public bool Power;
        private float Velocity;
        private Int32 HelicopterIndex;

        public bool Crashed { get; set; }

        private bool Menu;

        public Chopper()
            : base()
        {
            this.Enabled = true;
        }

        public Chopper(bool menu)
            : this()
        {
            Menu = menu;
        }

        public override void Load(OpenGameEngine.Base.GameCoreEventArgs e)
        {
            base.Load(e);

            this.AcceptsKeyboardInput = true;
            this.AcceptsMouseInput = true;
            this.Position = new Vector2(120, 282);
            Acceleration = 0.4f;
            BladeCounter = 0;
            BladeSpeed = 5;
            HelicopterImages = new Bitmap[4];

            Width = 73;
            Height = 36;

            for (int hi = 0; hi < HelicopterImages.Length; hi++)
            {
                using (FileStream fs = File.OpenRead("Assets\\helicopter" + (hi + 1).ToString() + ".png"))
                    HelicopterImages[hi] = Bitmap.LoadFromStream(fs);
            }

            // FlyingSound = AudioEngine.CreatePlayer("");
            MaxGravity = 7f;
            MaxVelocity = 6f;
            Power = false;
            Velocity = -1f;

            this.Visible = true;
        }

        public override void MouseDown(OpenGameEngine.Base.GameCoreEventArgs e, System.Windows.Forms.MouseButtons button)
        {
            base.MouseDown(e, button);

            if (button == System.Windows.Forms.MouseButtons.Left)
                Power = true;
        }

        public override void MouseUp(OpenGameEngine.Base.GameCoreEventArgs e, System.Windows.Forms.MouseButtons button)
        {
            base.MouseUp(e, button);

            if (button == System.Windows.Forms.MouseButtons.Left)
                Power = false;
        }

        public override void KeyDown(OpenGameEngine.Base.GameCoreEventArgs e, System.Windows.Forms.Keys key)
        {
            base.KeyDown(e, key);

            if (key == System.Windows.Forms.Keys.Space)
                Power = true;
        }

        public override void KeyUp(OpenGameEngine.Base.GameCoreEventArgs e, System.Windows.Forms.Keys key)
        {
            base.KeyUp(e, key);

            if (key == System.Windows.Forms.Keys.Space)
                Power = false;
        }

        public override void Update(OpenGameEngine.Base.GameCoreEventArgs e)
        {
            base.Update(e);

            UpdateHelicopter();
        }

        private void UpdateHelicopter()
        {
            if (!this.Crashed)
            {
                if (!Menu)
                {
                    if (this.Power)
                    {
                        this.BladeSpeed = 1;
                        this.Velocity -= this.Acceleration * 0.6f;

                        if (this.Velocity < -this.MaxVelocity)
                            this.Velocity = -this.MaxVelocity;
                    }
                    else
                    {
                        this.BladeSpeed = 5;
                        this.Velocity += this.Acceleration;

                        if (this.Velocity > this.MaxGravity)
                            this.Velocity = this.MaxGravity;
                    }

                    this.Y += this.Velocity;
                }

                this.BladeCounter++;
                if (this.BladeCounter >= this.BladeSpeed)
                {
                    this.BladeCounter = 0;
                    this.HelicopterIndex++;
                    if (this.HelicopterIndex >= this.HelicopterImages.Length - 1)
                        this.HelicopterIndex = 0;
                }
            }
            else
                this.HelicopterIndex = this.HelicopterImages.Length - 1;
        }

        public override void Draw(OpenGameEngine.Base.DrawEventArgs e)
        {
            base.Draw(e);

            e.DrawImage(HelicopterImages[HelicopterIndex], this);
        }
    }
}
